<?php

namespace turbo;

require_once EXTEND_PATH.'aliyun-openapi-php-sdk/aliyun-php-sdk-core/Config.php';
use think\Cache;
use app\common\model\ads\Ad;
use Mts\Request\V20140618 as Mts;
use Mts\Request\V20140618\SubmitSnapshotJobRequest;

class Video{


    /**
     * 处理广告的视频
     */
    public static function dealAd($id){
        $ad = Ad::get($id);

        if(!$ad || $ad->category != 1){
            return false;
        }

        $files = $ad->files;

        if(!$files){
            return false;
        }

        $files = explode(",", $files);

        if(count($files) > 1) return false;

        if(!isset($files[0]) || empty($files[0])) return false;

        $confg = get_addon_config('oss');

        $access_key_id = $confg['accessKeyId'];
        $access_key_secret = $confg['accessKeySecret'];
        $mps_region_id = 'cn-hangzhou';
        $pipeline_id = 'b21a3505bc524d49bfe388413a33401a';
        $oss_location = 'oss-cn-hangzhou';
        $oss_bucket = $confg['bucket'];
        $oss_input_object = self::getObject($files[0]);

        if(!$oss_input_object) return false;

        $oss_output_object = "uploads/snapshot/".(date('Ymd'))."/{$id}_{Count}.jpg";
        # DefaultAcsClient
        $clientProfile = \DefaultProfile::getProfile(
            $mps_region_id,                   # Region ID
            $access_key_id,                   # AccessKey ID
            $access_key_secret                # AccessKey Secret
        );
        $client = new \DefaultAcsClient($clientProfile);
        # request
        $request = new SubmitSnapshotJobRequest();
        $request->setAcceptFormat('JSON');
        # Input
        $input = array('Location' => $oss_location,
                       'Bucket' => $oss_bucket,
                       'Object' => urlencode($oss_input_object));
        $request->setInput(json_encode($input));
        # SnapshotConfig->OutputFile
        $output = array('Location' => $oss_location,
                        'Bucket' => $oss_bucket,
                        'Object' => urlencode($oss_output_object));
        $snapshot_config = array('OutputFile' => $output);
        # SnapshotConfig->Time
        $snapshot_config['Time'] = 2;
        # SnapshotConfig->Interval/Num
        $snapshot_config['Interval'] = 2;
        $snapshot_config['Num'] = 1;
        # SnapshotConfig->Width/Height
        $snapshot_config['Height'] = 360;
        # SnapshotConfig
        $request->setSnapshotConfig(json_encode($snapshot_config));
        # PipelineId
        $request->setPipelineId($pipeline_id);
        # call api
        try {
            $response = $client->getAcsResponse($request);
            if($response->{'RequestId'}){
                $files[1] = '/'.str_replace('{Count}','00001', $oss_output_object);
                $ad->files = implode(",", $files);
                return $ad->save();
            }
        } catch(\ServerException $e) {
            return false;
        } catch(\ClientException $e) {
            return false;
        }
        return false;
    }

    protected static function getObject($src){
        $info = parse_url($src);
        if(!isset($info['path'])){
            return '';
        }

        if(preg_match("/^\//", $info['path'])){
            return substr($info['path'], 1);
        }

        return $info['path'];
    }

}